/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 9/26/12
 * Time: 8:46 AM
 * To change this template use File | Settings | File Templates.
 */
package vplib.lab {
import alternativa.engine3d.objects.Mesh;

import mx.collections.ArrayCollection;
import mx.controls.Alert;

import vplib.component.AbstractPhysicsComponent;
import vplib.component.IPhysicsComponent;
import vplib.maple.UniqueIdUtil;
import vplib.model.AbstractPropertyModel;

public class Lab {
    private var labName:String;
    private var _physicsComponents:ArrayCollection;

    public function Lab() {
        this._physicsComponents = new ArrayCollection();
        trace("[Lab] \t- Create new instance with name '" + this.labName + "'.");
    }

    public function getLabName():String {
        return this.labName;
    }

    public function setLabName(name:String):void {
        this.labName = name;
    }

    public function get physicsComponents():ArrayCollection {
        trace("[Lab] \t- Return objects from lab '" + this.labName + "'.");
        return this._physicsComponents;
    }

    public function getComponentByUniqueId(name:String):IPhysicsComponent {
        for each (var comp:IPhysicsComponent in _physicsComponents) {
            if (comp.getUniqueId() == name)
                return comp;
        }
        return null;
    }

    public function getComponentBySurface(surface:Mesh):IPhysicsComponent {
        for each (var comp:IPhysicsComponent in _physicsComponents) {
            if (comp.getSurface() == surface)
                return comp;
        }
        return null;
    }

    public function addPhysicsComponent(apm:IPhysicsComponent):Boolean {
        if (getComponentByUniqueId(apm.getUniqueId()) == null) {
            var validateError = UniqueIdUtil.validateUniqueId(apm.getUniqueId());

            if (validateError == null) {
                apm.applyModelToSurface();
                this.physicsComponents.addItem(apm);
                trace("[Lab] \t- addPhysicsComponent -> " + apm.getComponentName());
                return true;
            } else {
                Alert.show(validateError, "Error adding component!", Alert.OK);
                trace("[Lab] \t- addPhysicsComponent - Error " + validateError);
                return false;
            }
        } else {
            Alert.show("Component with similar name already exists!", "Error adding component!", Alert.OK);
            return false;
        }
    }

    public function editPhysicsComponent(apm:IPhysicsComponent):Boolean {
        try {
            var position:int = -1;
            for each (var item:IPhysicsComponent in this.physicsComponents) {
                if (apm.getUniqueId() == item.getUniqueId())
                    position = this.physicsComponents.getItemIndex(item);
            }

            if (position != -1)
                this.physicsComponents.removeItemAt(position);
            else
                trace("[Lab] \t- editPhysicsComponent - You try edit not created component.")

            this.physicsComponents.addItem(apm);

            trace("[Lab] \t- editPhysicsComponent -> " + apm.getComponentName());
            return true;
        } catch (error:Error) {
            trace("[Lab] \t- editPhysicsComponent - Error = " + error.message);
            return false;
        }
        return false;
    }

    public function removePhysicsComponent(uniqueId:String):Boolean {
        try {
            var position:int = -1;
            for each (var item:AbstractPhysicsComponent in this.physicsComponents) {
                if (uniqueId == item.getUniqueId())
                    position = this.physicsComponents.getItemIndex(item);
            }

            if (position != -1)
                this.physicsComponents.removeItemAt(position);

            trace("[Lab] \t- Remove object -> " + uniqueId);
            return true;
        } catch (error:Error) {
            trace("[Lab] \t- removePhysicsComponent - Error = " + error.message);
            return false;
        }
        return false;
    }

    public function prepareComponents():void {
        for each (var item:IPhysicsComponent in this.physicsComponents) {
            item.applyModelToSurface();
        }
    }

    public function prepareComponent(item:IPhysicsComponent):Boolean {
        try {
            item.applyModelToSurface();
            return true;
        } catch (error:Error) {
            trace("[Lab] \t- removePhysicsComponent - Error = " + error.message);
            return false;
        }
        return false;
    }
}
}
